package com.dmm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.result.Result;
import com.dmm.user.entity.Apply;
import com.dmm.user.entity.Question;
import com.dmm.user.entity.Summary;
import com.dmm.user.service.SummaryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com/dmm
 * @since 2023-04-13
 */
@RestController
@CrossOrigin
@RequestMapping("/user/summary")
public class SummaryController {

    @Autowired
    SummaryService summaryService;

    @ApiOperation("添加报告")
    @PostMapping("/saveinfo")
    public Result saveinfo(@RequestBody Summary summary){
        System.out.println(summary.getReportName());
        boolean save = summaryService.save(summary);
        return Result.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("getpagelist/{page}/{limit}")
    public Result getpagelist(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "Summary", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) Summary summary) {
        //创建page对象，传递当前页和每页记录数
        Page<Summary> pageParam = new Page<>(page, limit);
        //获取条件值
        String reportName = summary.getReportName();//用户名
        String mintime = summary.getMintime();
        String maxtime = summary.getMaxtime();
        String report = summary.getReport();
        System.out.println(report);
        //封装条件
        QueryWrapper<Summary> wrapper = new QueryWrapper<>();
        wrapper.eq("email",summary.getEmail());
        if(!StringUtils.isEmpty(reportName)) {
            wrapper.like("report_name",reportName);
        }

        if(!StringUtils.isEmpty(mintime)) {
            wrapper.ge("update_time",mintime);
        }
        if(!StringUtils.isEmpty(maxtime)) {
            wrapper.le("update_time",maxtime);
        }
        if(!StringUtils.isEmpty(report)) {
            wrapper.like("report",report);
        }

        //调用方法得到分页查询结果
        IPage<Summary> pageModel = summaryService.page(pageParam, wrapper);
        System.out.println(pageModel);
        return Result.success(pageModel);
    }


    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteId/{id}")
    public Result deleteId(@PathVariable Long id){

        boolean b = summaryService.removeById(id);
        if(b){
            return Result.success(null);
        }
        return Result.fail(null);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/Batch_delete")
    public Result Batch_delete(@RequestBody List<Long> list){

        System.out.println(list);
        boolean b = summaryService.removeByIds(list);

        return b? Result.success(null):Result.fail(null);
    }

    @ApiOperation("根据id查报告")
    @GetMapping("/getbyid/{id}")
    public Result getbyid(@PathVariable Long id){

        System.out.println(id);

        Summary byId = summaryService.getById(id);


        return Result.success(byId);
    }

    @ApiOperation("根据id修改报告")
    @PutMapping("/update/{id}")
    public Result updateid(@PathVariable Integer id,
                           @RequestBody Summary summary){

        System.out.println(id);
        QueryWrapper<Summary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        summary.setId(id);
        summaryService.update(summary,queryWrapper);

        return Result.success(null);
    }


    @ApiOperation("分页查询admin")
    @PostMapping("getadminpagelist/{page}/{limit}")
    public Result getadminpagelist(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "Summary", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) Summary summary) {
        //创建page对象，传递当前页和每页记录数
        Page<Summary> pageParam = new Page<>(page, limit);
        //获取条件值
        String reportName = summary.getReportName();//用户名
        String mintime = summary.getMintime();
        String maxtime = summary.getMaxtime();
        String report = summary.getReport();
        String email = summary.getEmail();
        Integer id = summary.getId();
        String name = summary.getName();
        System.out.println(report);
        //封装条件
        QueryWrapper<Summary> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(id)) {
            wrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(email)) {
            wrapper.like("email",email);
        }
        if(!StringUtils.isEmpty(reportName)) {
            wrapper.like("report_name",reportName);
        }

        if(!StringUtils.isEmpty(mintime)) {
            wrapper.ge("update_time",mintime);
        }
        if(!StringUtils.isEmpty(maxtime)) {
            wrapper.le("update_time",maxtime);
        }
        if(!StringUtils.isEmpty(report)) {
            wrapper.like("report",report);
        }

        //调用方法得到分页查询结果
        IPage<Summary> pageModel = summaryService.page(pageParam, wrapper);
        System.out.println(pageModel);
        return Result.success(pageModel);
    }


    @ApiOperation("根据邮箱查询最新的报告")
    @GetMapping("/getcandidate/{email}")
    public Result getcandidate(@PathVariable String email){

        Summary summary=summaryService.getcandidate(email);
        return Result.success(summary);
    }

}

